package com.joseph.websocket.component;

import com.joseph.websocket.bean.SessionUser;
import io.netty.util.AttributeKey;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Log4j2
public class SessionManager {

    private static final Map<String, SessionUser> users = new ConcurrentHashMap<>();

    public static void addUser(SessionUser user) {
        log.info("USER {} ONLINE", user.getUserId());
        //final String userKey = String.format("s%_s%", user.getUserId(), user.getDevice());
        users.put(user.getUserId(), user);
    }

    public static void removeUser(String userId) {
        log.info("USER {} OFFLINE", userId);
        if (StringUtils.isNotEmpty(userId)) {
            users.remove(userId);
        }
    }

    public static SessionUser getUser(String userId) {
        return users.get(userId);
    }
}
